/* eslint no-magic-numbers: ["error", {
     "ignoreArrayIndexes": true,
     "ignore": [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,400, 401, 403, 404, 500, 10E3,10E7]
}] */
import path from 'path';
import views from 'koa-views';

import moment from 'moment';

export default function(env) {

    env.addGlobal('moment', moment);

    env.addFilter('json', function(str) {
        return env.filters.safe(JSON.stringify(str));
    });

    env.addFilter('duration', function(ss) {
        let mm = Math.floor(ss/60); //分钟
        ss = Math.floor(ss - mm * 60);
        mm = addZero(mm);
        ss = addZero(ss) 
        return `${mm}:${ss}`
    });

    env.addFilter('money', function(num) {
        return env.filters.safe(num.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'));
    });

    env.addFilter('toLocaleString',function(num){
        var a =num.toFixed(2);
        return a.toLocaleString();
    })

    env.addFilter('clear', function(obj) {
        return "";
    });

    // 判断是否为某种类型
    // 支持string,array,object
    env.addFilter('isstring', function(obj, type) {
        return typeof obj == 'string';
    });

    // 判断是否为数组
    env.addFilter('isarray', function(obj) {
        return obj instanceof Array;
    });

    // 判断是否为null
    env.addFilter('isnull', function(obj) {
        return obj == null;
    });

    // 校正数字 是整数的去掉.0 
    env.addFilter('Number', function(val) {
        return Number(val)
    });
    
    env.addFilter('replace', function(str,val) {
        if(!str) return str;
        return str.replace(val,"");
    });



    env.addFilter('moneyformat', function(amount, str) {
        let unit = '元';

        if (amount >= 10E3) {
            amount /= 10E3;
            unit = '万';
        } else if (amount >= 10E7) {
            amount /= 10E7;
            unit = '亿';
        }

        amount = str == 'nodigital'
            ? amount.toString()
            : amount.toFixed(2);

        let number = env.filters.safe(amount.replace(/(\d)(?=(\d{3})+\.)/g, '$1,'));
        return number.toString() + unit;
    });
  

   

    env.addFilter('JSON.parse', function(str) {
        return JSON.parse(str);
    });

    env.addFilter('join', function(str, sep1, sep2) {
        var coll = str.split(sep1);
        coll = _.filter(coll, function(ele) {
            return ele != ''
        }) || [];
        return coll.join(sep2);
    })

    env.addFilter('split', function(str, seperator) {
        if(!str) return str
        return str.split(seperator);
    })

    env.addFilter('slice', function(ary,...arg) {
        if(({}).toString.call(ary) != "[object Array]") return ary
        return ary.slice(...arg);
    })

    env.addFilter('parseInt', function(num) {
        return parseInt(num);
    })

    env.addFilter('parseFloat', function(num) {
        return parseFloat(num);
    })

   // 加密手机号
    env.addFilter('maskphone', function(phone,llen,rlen) {
        if(!phone || phone.length !== 11){
            return phone
        }
        let str = (new Array(11-llen-rlen)).fill("*").join("");
        
        return phone.substr(0,llen) + str + phone.slice(-rlen);
    });

    env.addFilter('decode_url', function(uri) {
        let redirectUrl = decodeURIComponent(uri);
        return redirectUrl;
    });

    //去除html标签
    env.addFilter('deleteHtml', function(content) {
        return content.replace(/<\/?[^<>]*>/g, "").replace(/&nbsp;/g, "");
    });

    //计算百分比
    env.addFilter('percent', function(num1,num2) {
        if(num2 === 0) return "0%";
        let percent = (num1 / num2);
        return Math.min(percent,1) * 100 + "%"
    });
    env.addFilter('mimeType', function(value){
        if(/.(png|jpg|jpeg|webp)$/i.test(value)){
            return "image"
        };
        if(/.(rm|mp4|rmvb|mtv|avi|flv|wmv|3gp)$/i.test(value)){
            return "video"
        }
        if(/.(m4a|mp3|wma|wav)$/i.test(value)){
            return "audio"
        }
        return "unknown"
    });
};

function addZero (num) {
    return num < 10 ? `0${num}` : num
}
